<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>
<script src="/bbs2/res/se2/js/HuskyEZCreator.js" type="text/javascript"></script>
<script src="/bbs2/res/js/mySE2.js" type="text/javascript"></script>
<style>
    input[name=title] { width:700px; border-style:groove; margin:2px; }
    form select { margin-right: 100px; }
    textarea { width: 766px; height: 600px; display: none; }
</style>
<script>
$(function() {
    $("button[type=submit]").click(function() {
        smartEditor_syncData();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    ArrayList<ArticleCategory> categories = ArticleCategoryDAO.selectAll(boardId);
    Board board = BoardDAO.selectById(boardId);

    String title = param.getString("title", "");
    int category = param.getInt("ct", 0);
    String body = param.getString("body", "");
    boolean notice = param.getBoolean("notice", false);
    
    if ("저장".equals(cmd)) {
        if (title.length() > 0) {
            if (body.length() > 0) {
                int articleId = param.getInt("aid", 0);
                if (articleId > 0)
                    ArticleDAO.createReply(boardId, articleId, category, title, body, notice, session);
                else
                    ArticleDAO.create(boardId, category, title, body, notice, session);
                response.sendRedirect("list.jsp?bid=" + boardId);
                return;
            } else
                errMsg = "내용을 입력하세요";            
        } else
            errMsg = "제목을 입력하세요";
    }
    String url = "list.jsp?" + request.getQueryString(); 
%>
<body>
<%@ include file="../_menu.jsp" %>  

<div class="container">

    <h1><%= board.getTitle() %></h1>
    <hr />
    
    <form method="post">
        <div>
            제목: <input type="text" name="title" value="<%= title %>" />            
        </div>
        
        <div class="form-inline">
            분류:
            <select name="ct">
                <% for (ArticleCategory ac : categories) { %>
                    <option value="<%= ac.getId() %>"><%= ac.getName() %></option>
                <% } %>                    
            </select>
            공지: <input type="checkbox" name="notice" /> 
        </div>
        
        <textarea id="articleBody" name="body" class="smarteditor2" rows="30" cols="100"><%= body %></textarea>
        
        <button type="submit" class="btn btn-primary" name="cmd" value="저장"><i class="icon-hdd icon-white"></i> 저장</button>
        <a href="<%= url %>" class="btn">취소</a>
    </form>
    
    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
</div>

<%@ include file="../_footer.jsp" %>  
</body>
</html>
